﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using TBlog.Web.Common.ValidateCode;
using TBlog.Web.Models;
using TBlog.Web.Repository;
using TBlog.Web.ViewModel;
using TBlog.Web.Common.PageHelper;
using TBlog.Web.Common.MD5;
namespace TBlog.Web.Service
{
    public class ManageService : BaseService<Users>, IManageService, IReadOnlySessionState
    {
        public IRepository<Users> _repository;
        public ManageService(IRepository<Users> repository)
            : base(repository)
        {
            _repository = repository;
        }


        public bool Valid(string UserName, string PassWord)
        {
            var md5pwd = MD5EncryptStr(PassWord);
            var res = _repository.GetAll().Select(a => a.UserName == UserName && a.PassWord == md5pwd).SingleOrDefault();
            //var SS = _repository.GetAll().ToPagedList(a => a.UserName == UserName && a.PassWord == PassWord, new PageListRequest(1, 2, false));

            return res;
        }
        public byte[] GetValidate(int num)
        {
            var vCode = new ValidateCodeHelper();
            string code = vCode.CreateValidateCode(num);
            HttpContext.Current.Session["VerifyCode"] = code;// Session["VerifyCode"] = code;
            return vCode.CreateValidateGraphic(code);
        }
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public string MD5EncryptStr(string str)
        {
            var MD5encrypt = new MD5encrypt();
            return MD5encrypt.MD5Encrypt(str);
        }

    }
}